package vip.huhaiong.hlfood.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
public class GetOpenId {
    @Autowired
    RestTemplate restTemplate;
    private String appId = "xxxxxxxxxxxxxxxxxxx";
    private String secretId = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
    private String url = "https://api.weixin.qq.com/sns/jscode2session";

    public JSONObject getOpenId(String code){
        String requestUrl = url+"?appid="+appId+"&secret="+secretId+"&js_code="+code+"&grant_type=authorization_code";
        String result = restTemplate.getForObject(requestUrl,String.class);
        log.info("openid:"+result);
        return JSONObject.parseObject(result);
    }
}
